﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion


using System.Collections.Generic;
using NUnit.Framework;
using System;
using System.Collections;
using NStl.SyntaxHelper;
using NStl.Linq;


namespace NStl.NUnit.Support
{
    /// <summary>
    /// A generic test fixture to test any <see cref="IDictionary{TKey,TValue}"/> implementation.
    /// </summary>
    /// <typeparam name="Key"></typeparam>
    /// <typeparam name="Value"></typeparam>
    public abstract class DictionaryTFixture<Key, Value> : CollectionTFixture<KeyValuePair<Key, Value>> where Key: class
    {
        protected sealed override ICollection<KeyValuePair<Key, Value>> CreateCollectionT()
        {
            return CreateDictionary();
        }
        protected IDictionary<Key, Value> DictionaryT
        {
            get { return (IDictionary<Key, Value>)EnumerableT; }
        }
        [SetUp]
        public override void Setup()
        {
            base.Setup();

            referenceArray = new KeyValuePair<Key, Value>[DictionaryT.Count];
            DictionaryT.CopyTo(referenceArray, 0);// Tested in base class!!
        }

        private KeyValuePair<Key, Value>[] referenceArray;
        /// <summary>
        /// When implemented, it creates a fille dictionary to test
        /// </summary>
        /// <returns></returns>
        protected abstract IDictionary<Key, Value> CreateDictionary();

        [Test]
        public void IndexerAccess()
        {
            foreach (KeyValuePair<Key, Value> item in referenceArray)
                Assert.AreEqual(item.Value, DictionaryT[item.Key]);
        }
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IndexerAccessKeyIsNull()
        {
            Value v = DictionaryT[null];
        }
        [Test]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void IndexerAccessKeyNotFound()
        {
            KeyValuePair<Key, Value> item = CreateNewUniqueItem();
            Value v = DictionaryT[item.Key];
        }

        [Test]
        public void IndexerAssignment()
        {
            KeyValuePair<Key, Value> item = CreateNewUniqueItem();

            Assert.AreNotEqual(item.Value, DictionaryT[referenceArray[0].Key]);
            DictionaryT[referenceArray[0].Key] = item.Value;
            Assert.AreEqual(item.Value, DictionaryT[referenceArray[0].Key]);

            DictionaryT[item.Key] = item.Value;
            Assert.AreEqual(item.Value, DictionaryT[item.Key]);
        }

        [Test]
        public void KeysProperty()
        {
            ICollection keys = DictionaryT.Keys.AsCollection();

            List<Key> expected = new List<Key>();
            Algorithm.Transform(referenceArray.Begin(), referenceArray.End(),
                                expected.AddLastInserter(), Select.FirstFromKeyValuePair<Key, Value>());

            CollectionAssert.AreEqual(expected, keys);
        }
        [Test]
        public void ValuesProperty()
        {
            ICollection values = DictionaryT.Values.AsCollection();

            List<Value> expected = new List<Value>();
            Algorithm.Transform(referenceArray.Begin(), referenceArray.End(),
                                expected.AddLastInserter(), Select.SecondFromKeyValuePair<Key, Value>());

            CollectionAssert.AreEqual(expected, values);
        }
        [Test]
        public void DictionaryAdd()
        {
            KeyValuePair<Key, Value> item = CreateNewUniqueItem();

            Assert.IsFalse(DictionaryT.ContainsKey(item.Key));
            DictionaryT.Add(item.Key, item.Value);
            Assert.IsTrue(DictionaryT.ContainsKey(item.Key));
        }
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DictionaryAddFailsKeyIsNull()
        {
            DictionaryT.Add(null, default(Value));
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public virtual void DictionaryAddFailsKeyAllreadyContained()
        {
            DictionaryT.Add(referenceArray[0].Key, default(Value));
        }

        [Test]
        public void ContainsKey()
        {
            Assert.IsTrue(DictionaryT.ContainsKey(referenceArray[1].Key));

            KeyValuePair<Key, Value> item = CreateNewUniqueItem();
            Assert.IsFalse(DictionaryT.ContainsKey(item.Key));
        }
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ContainsKeyWhenKeyIsNull()
        {
            bool unused = DictionaryT.ContainsKey(null);
        }

        [Test]
        public void DictionaryRemove()
        {
            Assert.IsTrue(DictionaryT.Remove(referenceArray[1].Key));
            Assert.AreEqual(referenceArray.Length - 1, DictionaryT.Count);
        }
        [Test]
        public void DictionaryRemoveFailsKeyNotPresent()
        {
            KeyValuePair<Key, Value> item = CreateNewUniqueItem();
            Assert.IsFalse(DictionaryT.ContainsKey(item.Key));
            Assert.AreEqual(referenceArray.Length, DictionaryT.Count);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DictionaryRemoveKeyIsNull()
        {
            Assert.IsTrue(DictionaryT.Remove(null));
        }

        [Test]
        public void TryGetValue()
        {
            Value v;
            Assert.IsTrue(DictionaryT.TryGetValue(referenceArray[1].Key, out v));
            Assert.AreEqual(referenceArray[1].Value, v);
        }
        [Test]
        public void TryGetValueFailsKeyNotPresent()
        {
            KeyValuePair<Key, Value> item = CreateNewUniqueItem();
            Value v;
            Assert.IsFalse(DictionaryT.TryGetValue(item.Key, out v));
            Assert.AreEqual(default(Value), v);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TryGetValueFailsKeyIsNull()
        {
            Value v;
            Assert.IsFalse(DictionaryT.TryGetValue(null, out v));
        }
    }
}
